package tchk.xyz.lingyuntutorminiprogram.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import tchk.xyz.lingyuntutorminiprogram.dto.OrderAdminDTO;
import tchk.xyz.lingyuntutorminiprogram.service.IOrdersService;
import tchk.xyz.lingyuntutorminiprogram.utils.Result;

@RestController
@RequestMapping("/api/admin/order")
@PreAuthorize("hasAuthority('admin:order:monitor')")
public class AdminOrderController {

    @Autowired
    private IOrdersService ordersService;

    @GetMapping("/list")
    public Result<IPage<OrderAdminDTO>> getOrderList(@RequestParam(defaultValue = "1") long current,
                                                     @RequestParam(defaultValue = "10") long size,
                                                     @RequestParam(required = false) String orderNo) {
        Page<OrderAdminDTO> page = new Page<>(current, size);
        return Result.success(ordersService.getOrderListForAdmin(page, orderNo));
    }

    @PutMapping("/{orderId}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long orderId, @RequestParam String reason) {
        ordersService.cancelOrderByAdmin(orderId, reason);
        return Result.success();
    }
}